﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormProject
{
    public partial class HPLAN325 : Form
    {
        //-------------------   D E Ğ İ Ş K E N L E R 
        int etkilenen;
        string NOKTA1, NOKTA2;
        String HESAPNO1, HESAPNO2, HESAPNO3;
        //-------------------   D E Ğ İ Ş K E N L E R    S O N U 
        SqlConnection cnn = new SqlConnection("Server = .; Database=proje;trusted_connection=true;");
        public HPLAN325()
        {
            InitializeComponent();
        }
        //----------------------------------------------

        private void Temizle()
        { 
            foreach (Control ITEM in this.Controls)
            {
                if (ITEM is TextBox | ITEM is MaskedTextBox)
                {
                    ITEM.ResetText();
                }
            }
              
        }
        private void ListViewDoldur()
        {
            Temizle();
            listView1.Items.Clear();
            
            SqlCommand cmd = new SqlCommand("Select * From HesapPlani", cnn);
            
            cnn.Open();
            SqlDataReader rdr = cmd.ExecuteReader();

            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.Text = rdr["HesapNo"].ToString();
                    lvi.SubItems.Add(rdr["HesapAciklama"].ToString());
                    listView1.Items.Add(lvi);
                }
            }
            else
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = "---";
                lvi.SubItems.Add("Kayit Bulunamadı");
                listView1.Items.Add(lvi);
            }
            rdr.Close();
            rdr.Dispose();

            cnn.Close();
        }

        //----------------------------------------------
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
           
            if (listView1.SelectedItems.Count > 0)
            {
                Temizle();
                DEGISKEN_BOSALT();


                ListViewItem lvi = listView1.SelectedItems[0];
                Lbl_HesapNo.Text = lvi.Text;
                Lbl_HesapAciklama.Text = lvi.SubItems[1].Text;
                string HESAPNO = lvi.SubItems[0].Text;
                try
                {
                    string[] dizi = HESAPNO.Split('.');

                    if (dizi.Count() == 3)
                    {
                        HESAPNO1 = dizi[0];
                        HESAPNO2 = dizi[1];
                        HESAPNO3 = dizi[2];
                    }
                    else if (dizi.Count() == 2)
                    {
                        HESAPNO1 = dizi[0];
                        HESAPNO2 = dizi[1];
                        HESAPNO3 = null;
                    }
                    else if (dizi.Count() == 1)
                    {
                        HESAPNO1 = dizi[0];
                        HESAPNO2 = null;
                        HESAPNO3 = null;
                    }
                }
                catch 
                {
                }
               
                MTXT_HESAPNO.Text = HESAPNO1;
                MTXT_HESAPNO_TALI.Text = HESAPNO2;
                MTXT_HESAPNO_DETAY.Text = HESAPNO3;
               
                TXT_HESAPACIKLAMA.Text = lvi.SubItems[1].Text;

            }
           
        }

        private void HPLAN325_Load(object sender, EventArgs e)
        {
            ListViewDoldur();
        }

        private void BTN_CIKIS_Click(object sender, EventArgs e)
        {
             this.Close();
        }

        private void BTN_YENI_KAYIT_Click(object sender, EventArgs e)
        {
            Temizle();
            DEGISKEN_BOSALT();
            MTXT_HESAPNO.Focus();

        }

        private void BTN_KAYDET_Click(object sender, EventArgs e)
        {
            if (MTXT_HESAPNO.Text == "")
            {
                MessageBox.Show("Lütfen Hesap Noyu boş geçmeyiniz");
                MTXT_HESAPNO.Focus();
                return;
            }
            else if (TXT_HESAPACIKLAMA.Text == "")
            {
                MessageBox.Show("Lütfen Hesap Adını boş geçmeyiniz");
                TXT_HESAPACIKLAMA.Focus();
                return;
            }
            //----------------------------------------------
            NOKTA1 = "";
            NOKTA2 = "";
            if (MTXT_HESAPNO_TALI.Text != "" )
            {
                 NOKTA1 = "." ;
            }
            if (MTXT_HESAPNO_DETAY.Text != "")
            {
                NOKTA2 = ".";
            }
               
         
            try
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO HesapPlani (HesapNo ,HesapAciklama) VALUES (@HesapNo,@HesapAciklama)", cnn);
                cmd.Parameters.Add("@HesapNo", SqlDbType.NVarChar).Value = MTXT_HESAPNO.Text + NOKTA1 + MTXT_HESAPNO_TALI.Text + NOKTA2 + MTXT_HESAPNO_DETAY.Text;
                cmd.Parameters.Add("@HesapAciklama", SqlDbType.NVarChar).Value = TXT_HESAPACIKLAMA.Text;
                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();
                MessageBox.Show("1 ADET KAYIT YAPILMIŞTIR");
            }
            catch (Exception ex)
            {
                cnn.Close();
                MessageBox.Show(ex.Message);
            }
            ListViewDoldur();
            DEGISKEN_BOSALT();
            Temizle();
            MTXT_HESAPNO.Focus();
            //----------------------------------------------
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

            BUTON_GIZLE();
            TXT_HESAPACIKLAMA.Focus();
           
            

        }
       
        private void BTN_DEGISTIR_Click(object sender, EventArgs e)
        {
            
            SqlCommand cmd_UPDATE = new SqlCommand("UPDATE HesapPlani SET HesapAciklama =@HesapAciklama WHERE HesapNo =@HesapNo", cnn);
            SqlParameter prm = new SqlParameter("@HesapAciklama", SqlDbType.NVarChar, 250);
            cmd_UPDATE.Parameters.AddWithValue("@HesapNo",MTXT_HESAPNO.Text);
            prm.Value = TXT_HESAPACIKLAMA.Text;
            cmd_UPDATE.Parameters.Add(prm);

            
            cnn.Open();
            try
            {
            etkilenen = cmd_UPDATE.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                cnn.Close();
                MessageBox.Show(ex.Message);
            }

            if (etkilenen > 0)
            {
                MessageBox.Show("Güncelleme Tamamdır.");
            }
            else
            {
                MessageBox.Show("Güncelleme Olmadı.");
            }

            cnn.Close();

            BUTON_GOSTER();
            ListViewDoldur();
        }
         private void BUTON_GIZLE()
        {
            BTN_YENI_KAYIT.Visible = false;
            BTN_KAYDET.Visible = false;
            BTN_SIL.Visible = false;
        }
         private void BUTON_GOSTER()
         {
             BTN_YENI_KAYIT.Visible = true;
             BTN_KAYDET.Visible = true;
             BTN_SIL.Visible = true;
         }
         private void DEGISKEN_BOSALT()
         {
             MTXT_HESAPNO.Text = "";
             MTXT_HESAPNO_TALI.Text = "";
             MTXT_HESAPNO_DETAY.Text = "";
             TXT_HESAPACIKLAMA.Text = "";
         }

      

         private void MTXT_HESAPNO_KeyPress(object sender, KeyPressEventArgs e)
         {
             if (MTXT_HESAPNO.Text.Length > 2)
             {
                 MTXT_HESAPNO_TALI.Focus();
                 MTXT_HESAPNO_TALI.Text += e.KeyChar.ToString();
             }
         }
         private void MTXT_HESAPNO_TALI_KeyPress(object sender, KeyPressEventArgs e)
         {
             if (MTXT_HESAPNO_TALI.Text.Length > 1)
             {
                 MTXT_HESAPNO_DETAY.Focus();
                 MTXT_HESAPNO_DETAY.Text += e.KeyChar.ToString();
             }
         }
         private void MTXT_HESAPNO_DETAY_KeyPress(object sender, KeyPressEventArgs e)
         {
             if (MTXT_HESAPNO_DETAY.Text.Length > 4)
             {
                 TXT_HESAPACIKLAMA.Focus();
             }
         }

         private void BTN_SIL_Click(object sender, EventArgs e)
         {
             if (MessageBox.Show("Silmek istediğnize Eminmisiniz ? ", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
             {

                 SqlCommand cmd_DELETE = new SqlCommand("DELETE HesapPlani  WHERE HesapNo =@HesapNo", cnn);
                 SqlParameter prm = new SqlParameter("@HesapNo", SqlDbType.NVarChar, 250);
               
                 prm.Value = MTXT_HESAPNO.Text + "." + MTXT_HESAPNO_TALI.Text +"."+ MTXT_HESAPNO_DETAY.Text;
                 cmd_DELETE.Parameters.Add(prm);


                 cnn.Open();
                 try
                 {
                     etkilenen = cmd_DELETE.ExecuteNonQuery();
                 }
                 catch (Exception ex)
                 {
                     cnn.Close();
                     MessageBox.Show(ex.Message);
                 }
                 cnn.Close();
                 if (etkilenen > 0)
                 {
                     MessageBox.Show(" S İ L İ N D İ");
                     Temizle();
                    
                     ListViewDoldur();
                 }
                 else
                 {
                     MessageBox.Show("SİLİNMEDİ");
                 }

                
               
             }
            
         }

         

    }
}
